package order.service.impl;

import java.util.List;

import order.dao.IOrderDAO;
import order.model.OpenTable;
import order.model.OrderItem;
import order.service.IOrderService;

import common.bean.PageInfo;
import common.exception.BusinessException;
import common.service.impl.GenericService;

public class OrderService extends GenericService implements IOrderService
{
	private IOrderDAO orderDAO;
	
	@Override
	public OpenTable getOpenTable(int orderId) throws BusinessException
	{
		try
		{
			return orderDAO.getOpenTable(orderId);
		}
		catch(Exception e)
		{
			throw new BusinessException("failed to query OpenTableOrder.", e);
		}
	}
	


	@Override
	public List<OpenTable> queryOpenTables(PageInfo page)
			throws BusinessException
	{
		try
		{
			return orderDAO.list(page);
		}
		catch(Exception e)
		{
			throw new BusinessException("failed to query OpenTableOrder.", e);
		}
	}

	@Override
	public List<OrderItem> getOrderItems(int orderId) throws BusinessException
	{
		try
		{
			return orderDAO.getOrderItems(orderId);
		}
		catch(Exception e)
		{
			throw new BusinessException("failed to query order items.", e);
		}
	}

	@Override
	public void saveOrder(OpenTable tableOrder) throws BusinessException
	{
		int orderId = -1;
		try
		{
			orderId = orderDAO.saveOpenTable(tableOrder);
		}
		catch(Exception e)
		{
			throw new BusinessException("failed to save opentable entity.", e);
		}
		
		if(tableOrder.getOrderItems().size() > 0)
		{
			try
			{
				orderDAO.saveOrderItems(tableOrder.getOrderItems());
				return ;//success
			}
			catch(Exception e)
			{
				log.error(e);
			}
			
			orderDAO.deleteOpenTable(orderId);
		}
	}

	public IOrderDAO getOrderDAO()
	{
		return orderDAO;
	}

	public void setOrderDAO(IOrderDAO orderDAO)
	{
		this.orderDAO = orderDAO;
	}
	
}
